unit Unit35; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, Unit36, Unit1,
  sqldb, db, DBGrids, Unit37, Process, Unit16, LR_Class, LR_DBSet, Unit12,
  lNetComponents, lNet, DbCtrls;

type

  { TForm35 }

  TForm35 = class(TForm)
    Button1: TButton;
    Button10: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    ComboBox1: TComboBox;
    Datasource1: TDatasource;
    Datasource2: TDatasource;
    DBComboBox1: TDBComboBox;
    DBGrid1: TDBGrid;
    Edit1: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit12: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    GroupBox4: TGroupBox;
    GroupBox5: TGroupBox;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    LTCP: TLTCPComponent;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    RadioButton5: TRadioButton;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    SQLQuery3: TSQLQuery;
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure DBComboBox1Select(Sender: TObject);
    procedure Edit12Change(Sender: TObject);
    procedure Edit5Exit(Sender: TObject);
    procedure Edit5KeyPress(Sender: TObject; var Key: char);
    procedure facturar();
    procedure abrir_venta();
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow;
    procedure frReport1GetValue(const ParName: String; var ParValue: Variant);
    procedure LTCPReceive(aSocket: TLSocket);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton3Click(Sender: TObject);
    procedure RadioButton4Click(Sender: TObject);
    procedure RadioButton5Click(Sender: TObject);
  private
    descuento : string;
    descuento_cantidad : string;
    forma_pago : string;
    item : longint;
    nro_conexion : string;
    archivo_fiscal : string;
    paso : longint;
    subtotal : string;
    total_iva : string;
    pago_cliente : string;
    broker : string;
    saldo_cliente : real;
  public
    { public declarations }
  end; 

var
  Form35: TForm35; 

implementation

{ TForm35 }

procedure TForm35.abrir_venta();
var
  QueryString : String;

begin
  if Label6.Caption = '' then
    if broker <> '' then
      QueryString := 'insert into venta(vta_cliente, vta_funcionario, vta_sucursal, vta_broker) values('
      + Edit1.Text + ', ' + '(select fnc_cdg from funcionario where fnc_nombre = '''
      + DBComboBox1.text + '''), ' + registro.codigo_sucursal + ', ' + broker + ')'
    else
      QueryString := 'insert into venta(vta_cliente, vta_funcionario, vta_sucursal) values('
      + Edit1.Text + ', ' + '(select fnc_cdg from funcionario where fnc_nombre = '''
      + DBComboBox1.text + '''), ' + registro.codigo_sucursal + ')'
  else
    QueryString := 'update venta set vta_cliente = ' + Edit1.Text + ' where vta_cdg = '
      + Label6.Caption;

  Form1.PQConnection1.ExecuteDirect('begin work;');
  Form1.PQConnection1.ExecuteDirect(QueryString);
  Form1.PQConnection1.ExecuteDirect('commit work;');

  SQLQuery1.SQL.Text := 'select max(vta_cdg) from venta where vta_cliente = ' + Edit1.Text;

  SQLQuery1.Open;

  while not SQLQuery1.EOF do
  begin
    Label6.Caption := SQLQuery1.Fields[0].AsString;
    SQLQuery1.Next;
  end;

  SQLQuery1.Close;

  SQLQuery2.SQL.Text := 'select cantidad, prd_nombre, punitario, precio, prv_iva, prv_impint, '
    + 'punitario_siva, precio_siva, total_iva from vw_producto_venta where prv_venta = '
    + Label6.Caption + ' order by prv_cdg';

  SQLQuery2.Close;

  Edit5.Enabled := True;
  Edit6.Enabled := True;
  Edit7.Enabled := True;
  Edit8.Enabled := True;

  Button2.Enabled := True;
  Button3.Enabled := True;
  Button4.Enabled := True;
  Button5.Enabled := True;
  Button8.Enabled := True;
  
  RadioButton1.Enabled := True;
  RadioButton2.Enabled := True;
  RadioButton3.Enabled := True;
  RadioButton4.Enabled := True;
  RadioButton5.Enabled := True;
  
  RadioButton1.Checked := True;

  ComboBox1.Enabled := True;
  ComboBox1.Text := 'Lista 1';
  
  Edit5.SetFocus;
end;

procedure TForm35.facturar();
begin
  if registro.tipo_impresor <> 'Ninguno' then
  begin
    nro_conexion := '';
    archivo_fiscal := '';
    paso := 1;
  
    if RadioButton1.Checked = True then
      forma_pago := 'Efectivo'
    else
    if RadioButton2.Checked = True then
      forma_pago := 'Cuenta Corriente'
    else
    if RadioButton3.Checked = True then
      forma_pago := 'Tarjeta'
    else
    if RadioButton4.Checked = True then
      forma_pago := 'Cheque'
    else
    if RadioButton5.Checked = True then
      forma_pago := 'Debito';

    LTCP.Connect(registro.ip_impresor, puertoTCP);
  end
  else
  if Edit9.Text = 'I' then
  begin
    frReport1.LoadFromFile('factura_a.lrf');
    frReport1.ShowReport;
    Close;
  end
  else
  begin
    frReport1.LoadFromFile('factura_bc.lrf');
    frReport1.ShowReport;
    Close;
  end
end;

procedure TForm35.Button7Click(Sender: TObject);
begin
  Form16.ShowModal;
  
  Edit2.Text := Form16.Edit1.Text;
  Edit3.Text := Form16.Edit2.Text;
  Edit4.Text := Form16.Edit3.Text;
  Edit1.Text := Form16.codigo;
  Edit9.Text := Form16.iva;
  Edit11.Text := Form16.Edit6.Text;
  
  abrir_venta;
end;

procedure TForm35.Button8Click(Sender: TObject);
begin
  Form12.ShowModal;
  Edit5.Text := Form12.codigo;

  if ComboBox1.Text = 'Lista 1' then
    Edit7.Text := FormatFloat('###0.00', StrToFloat(Form12.precio1))
  else
  if ComboBox1.Text = 'Lista 2' then
    Edit7.Text := FormatFloat('###0.00', StrToFloat(Form12.precio2))
  else
  if ComboBox1.Text = 'Lista 3' then
    Edit7.Text := FormatFloat('###0.00', StrToFloat(Form12.precio3))
  else
  if ComboBox1.Text = 'Lista 4' then
    Edit7.Text := FormatFloat('###0.00', StrToFloat(Form12.precio4));

  Label12.Caption := Form12.nombre;
  
  Edit6.Text := '1';
  Edit6.SetFocus;
end;

procedure TForm35.Button9Click(Sender: TObject);
begin
  Edit1.Text := '1';
  Edit2.Text := 'CONSUMIDOR FINAL';
  Edit3.Text := '00000000000';
  Edit4.Text := '--';
  Edit9.Text := 'C';
  
  abrir_venta;
end;

procedure TForm35.DBComboBox1Select(Sender: TObject);
var
  QueryString : string;
begin
  if Label6.Caption = '' then
  begin
    Button1.Enabled := True;
    Button7.Enabled := True;
    Button9.Enabled := True;
  
    Button10.SetFocus;
  end
  else
  begin
    QueryString := 'update venta set vta_funcionario = (select fnc_cdg from funcionario where '
      + 'fnc_nombre = ''' + DBComboBox1.Text + ''') where vta_cdg = ' + Label6.Caption;
      
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
    
    Edit5.SetFocus;
  end;
end;

procedure TForm35.Edit12Change(Sender: TObject);
var
  QueryString : string;

begin
  if Label6.Caption <> '' then
  begin
    QueryString := 'update venta set vta_broker = '
      + broker + ' where vta_cdg = ' + Label6.Caption;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
  end;
end;

procedure TForm35.Edit5Exit(Sender: TObject);
begin
  if Edit5.Text <> '' then
  begin
    if SQLQuery1.Active = true
      then SQLQuery1.Close;
    
    SQLQuery1.SQL.Text := 'select prd_nombre, ';

    if ComboBox1.Text = 'Lista 1' then
      SQLQuery1.SQL.Text := SQLQuery1.SQL.Text + 'prd_precio1'
    else
    if ComboBox1.Text = 'Lista 2' then
      SQLQuery1.SQL.Text := SQLQuery1.SQL.Text + 'prd_precio2'
    else
    if ComboBox1.Text = 'Lista 3' then
      SQLQuery1.SQL.Text := SQLQuery1.SQL.Text + 'prd_precio3'
    else
    if ComboBox1.Text = 'Lista 4' then
      SQLQuery1.SQL.Text := SQLQuery1.SQL.Text + 'prd_precio4';
      
    SQLQuery1.SQL.Text := SQLQuery1.SQL.Text + ' from producto where prd_cdg = ' + Edit5.Text;

    if SQLQuery1.Active = true then
      SQLQuery1.Close;

    SQLQuery1.Open;
  
    while not SQLQuery1.EOF do
    begin
      Label12.Caption := SQLQuery1.Fields[0].AsString;
      Edit7.Text := FormatFloat('###0.00', StrToFloat(SQLQuery1.Fields[1].AsString));
      Edit6.Text := '1';
      SQLQuery1.Next;
    end;
  
    SQLQuery1.Close;
  end;
end;

procedure TForm35.Edit5KeyPress(Sender: TObject; var Key: char);
begin
  if Key = chr(13) then
    Edit6.SetFocus;
end;

procedure TForm35.Button10Click(Sender: TObject);
begin
    Form36.ShowModal;

  if Form36.codigo <> '' then
  begin
    broker := Form36.codigo;
    Edit12.Text := Form36.nombre;
  end;
end;

procedure TForm35.Button1Click(Sender: TObject);
begin
  Form36.ShowModal;

  if Form36.codigo <> '' then
  begin
    Edit1.Text := Form36.codigo;
    Edit2.Text := Form36.nombre;
    Edit3.Text := Form36.cuit;
    Edit9.Text := Form36.iva;
    Edit4.Text := Form36.direccion;
    Edit11.Text := Form36.region;
    abrir_venta;
  end;

  SQLQuery1.SQL.text := 'select saldo from vw_cliente where cln_cdg = ' + Edit1.Text;

  SQLQuery1.Open;

  while not SQLQuery1.EOF do
  begin
    saldo_cliente := SQLQuery1.Fields[0].AsFloat;
    SQLQuery1.Next;
  end;

  SQLQuery1.Close;

end;

procedure TForm35.Button2Click(Sender: TObject);
begin
  Form37.ShowModal;

  if Form37.codigo_producto <> '' then
  begin
    Edit5.Text := Form37.codigo_producto;
    Edit6.Text := '1';
    
    if ComboBox1.Text = 'Lista 1' then
      Edit7.Text := FormatFloat('###0.00', StrToFloat(Form37.precio1))
    else
    if ComboBox1.Text = 'Lista 2' then
      Edit7.Text := FormatFloat('###0.00', StrToFloat(Form37.precio2))
    else
    if ComboBox1.Text = 'Lista 3' then
      Edit7.Text := FormatFloat('###0.00', StrToFloat(Form37.precio3))
    else
    if ComboBox1.Text = 'Lista 4' then
      Edit7.Text := FormatFloat('###0.00', StrToFloat(Form37.precio4));
      
    Label12.Caption := Form37.nombre_producto;
    
    Edit6.SetFocus;
  end;
end;

procedure TForm35.Button3Click(Sender: TObject);
var
  QueryString : string;

begin
  if Edit5.Text <> '' then
  begin
    if LeftStr(Edit6.Text, 1) = '$' then
      Edit6.Text := FloatToStr(StrToFloat(RightStr(Edit6.Text, Length(Edit6.Text) - 1)) /
        StrToFloat(Edit7.Text));

    QueryString := 'select vender(' + Label6.Caption + ', ' + Edit5.Text
      + ', ' + Edit6.Text + ', ' + Edit7.Text + ')';

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    
    Edit5.Text := '';
    Edit6.Text := '';
    Edit7.Text := '';
  
    if SQLQuery2.Active = False then
      SQLQuery2.Open
    else
    begin
      SQLQuery2.Close;
      SQLQuery2.Open;
    end;
  
    DBGrid1.Columns[0].Title.Caption := 'Cant.';
    DBGrid1.Columns[1].Title.Caption := 'Producto';
    DBGrid1.Columns[2].Title.Caption := 'Pcio. Unitario';
    DBGrid1.Columns[3].Title.Caption := 'Importe';

    DBGrid1.Columns[0].Width := 50;
    DBGrid1.Columns[1].Width := 305;
    DBGrid1.Columns[2].Width := 80;
    DBGrid1.Columns[3].Width := 80;
    
    DBGrid1.Columns[4].Visible := false;
    DBGrid1.Columns[5].Visible := false;
    DBGrid1.Columns[6].Visible := false;
    DBGrid1.Columns[7].Visible := false;
    DBGrid1.Columns[8].Visible := false;

    TColumn(DBGrid1.Columns[2]).DisplayFormat := '$ ###0.00';
    TColumn(DBGrid1.Columns[3]).DisplayFormat := '$ ###0.00';
    TColumn(DBGrid1.Columns[4]).DisplayFormat := '###0.0';
    
    if SQLQuery1.Active = True then
      SQLQuery1.Close;
    
    SQLQuery1.SQL.Text := 'select vta_total, vta_total_siva, vta_total_iva from venta where vta_cdg = '
      + Label6.Caption;
  
    SQLQuery1.Open;
  
    while not SQLQuery1.EOF do
    begin
      Edit8.Text := FormatFloat('###0.00', SQLQuery1.Fields[0].AsFloat);
      subtotal := SQLQuery1.Fields[1].AsString;
      total_iva := SQLQuery1.Fields[2].AsString;
      SQLQuery1.Next;
    end;
  
    SQLQuery1.Close;
  
    Label12.Caption := '';
    
    if Button6.Enabled = false then
      Button6.Enabled := True;
      
    Edit5.SetFocus;
  end;
end;

procedure TForm35.Button4Click(Sender: TObject);
var
  QueryString : string;

begin
  QueryString := 'delete from producto_venta where prv_cdg = (select max(prv_cdg) '
    + 'from producto_venta where prv_venta = ' + Label6.Caption + ')';
    
  Form1.PQConnection1.ExecuteDirect('begin work;');
  Form1.PQConnection1.ExecuteDirect(QueryString);
  Form1.PQConnection1.ExecuteDirect('commit work;');

  Edit5.Text := '';
  Edit6.Text := '';
  Edit7.Text := '';

  if SQLQuery2.Active = False then
    SQLQuery2.Open
  else
  begin
    SQLQuery2.Close;
    SQLQuery2.Open;
  end;

  DBGrid1.Columns[0].Title.Caption := 'Cant.';
  DBGrid1.Columns[1].Title.Caption := 'Producto';
  DBGrid1.Columns[2].Title.Caption := 'Pcio. Unitario';
  DBGrid1.Columns[3].Title.Caption := 'Importe';
  DBGrid1.Columns[4].Visible := false;

  DBGrid1.Columns[0].Width := 50;
  DBGrid1.Columns[1].Width := 305;
  DBGrid1.Columns[2].Width := 80;
  DBGrid1.Columns[3].Width := 80;


  DBGrid1.Columns[4].Visible := false;
  DBGrid1.Columns[5].Visible := false;
  DBGrid1.Columns[6].Visible := false;
  DBGrid1.Columns[7].Visible := false;
  DBGrid1.Columns[8].Visible := false;

  TColumn(DBGrid1.Columns[2]).DisplayFormat := '$ ####0.00';
  TColumn(DBGrid1.Columns[3]).DisplayFormat := '$ ####0.00';

  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select vta_total from venta where vta_cdg = ' + Label6.Caption;

  SQLQuery1.Open;

  while not SQLQuery1.EOF do
  begin
    Edit8.Text := FormatFloat('###0.00', SQLQuery1.Fields[0].AsFloat);
    SQLQuery1.Next;
  end;

  SQLQuery1.Close;

  Edit5.SetFocus;
end;

procedure TForm35.Button5Click(Sender: TObject);
begin
  descuento := InputBox('Descuento', 'Ingrese porcentaje a descontar(-) / recargar(+) (%):', '');
  
  if descuento <> '' then
  begin

    if SQLQuery1.Active = True then
      SQLQuery1.Close;
      
    SQLQuery1.SQL.Text := 'select descontar(' + label6.Caption + ', ' + FormatFloat('###0.00', StrToFloat(descuento)) + ')';

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      descuento_cantidad := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next;
    end;

    SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'select vta_total from venta where vta_cdg = ' + Label6.Caption;

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      Edit8.Text := FormatFloat('###0.00', SQLQuery1.Fields[0].AsFloat);
      SQLQuery1.Next;
    end;

    SQLQuery1.Close;
    
    ShowMessage('Aplicado ' + descuento + '% de descuento / recargo sobre el total de la venta');
    
    Button2.Enabled := False;
    Button3.Enabled := False;
    Button4.Enabled := False;
    Button5.Enabled := False;
    Button8.Enabled := False;
    
    Edit5.Enabled := False;
    Edit6.Enabled := False;
    Edit7.Enabled := False;
  end;
end;

procedure TForm35.Button6Click(Sender: TObject);
var
  QueryString : String;
  cambio : String;

begin
  if Edit10.Text <> '' then
  begin
    QueryString := 'update venta set vta_detalle = ''' + Edit10.Text + ''' where vta_cdg = '
      + Label6.Caption;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
  end;

  if RadioButton1.Checked = true then
  begin
    pago_cliente := InputBox('Pago del Cliente', 'Ingrese pago del cliente:', '');
  
    if pago_cliente = '' then
      pago_cliente := Edit8.Text
    else
      pago_cliente := FormatFloat('###0.00', StrToFloat(pago_cliente));
      cambio := FormatFloat('$###0.00', StrToFloat(pago_cliente) - StrToFloat(Edit8.Text));
      ShowMessage('Cambio: ' + cambio);
  end
  else
     pago_cliente := Edit8.Text;

  facturar();
end;

procedure TForm35.FormClose(Sender: TObject; var CloseAction: TCloseAction);
var
  imprimir_nota : Integer;
  QueryString : string;
  
begin
  if (Edit8.Text = '0') and (Label6.Caption <> '') then
  begin
    QueryString := 'delete from venta where vta_cdg = ' + Label6.Caption;
    
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
  end
  else
  begin
    if (RadioButton2.Checked = True) and (Edit1.text <> '') then
    begin
      QueryString := 'insert into resumen_cuenta(rsm_cliente, rsm_descripcion, rsm_operacion, rsm_debe) '
        + 'values(' + Edit1.Text + ', ''COMPRA CTACTE'', ' + Label6.Caption + ', ' + Edit8.Text + ')';

      Form1.PQConnection1.ExecuteDirect('begin work;');
      Form1.PQConnection1.ExecuteDirect(QueryString);
      Form1.PQConnection1.ExecuteDirect('commit work;');

      if saldo_cliente < 0 then
      begin
        if (-1 * (saldo_cliente) <= strtofloat(Edit8.Text)) then
          Edit8.Text := floattostr(strtofloat(Edit8.Text) + saldo_cliente)
        else
          Edit8.Text := '0';
      end;

      QueryString := 'update venta set vta_saldo = ' + Edit8.Text + ' where vta_cdg = ' + Label6.Caption;

      Form1.PQConnection1.ExecuteDirect('begin work;');
      Form1.PQConnection1.ExecuteDirect(QueryString);
      Form1.PQConnection1.ExecuteDirect('commit work;');
    end;

    if label6.Caption <> '' then
    begin
      imprimir_nota := MessageDlg('Imprimir Nota de Entrega?',mtConfirmation, mbOKCancel, 0);

      if imprimir_nota = mrOK then
      begin
        frReport1.LoadFromFile('nota_entrega.lrf');
        frReport1.ShowReport;
      end;
    end;
  end;


  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  if SQLQuery2.Active = True then
    SQLQuery2.Close;
    
  if SQLQuery3.Active = True then
    SQLQuery3.Close;

  Label6.Caption := '';
end;

procedure TForm35.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
  
  SQLQuery2.DataBase := Form1.PQConnection1;
  SQLQuery2.Transaction := Form1.SQLTransaction1;
  
  SQLQuery3.DataBase := Form1.PQConnection1;
  SQLQuery3.Transaction := Form1.SQLTransaction1;
end;

procedure TForm35.FormShow;
begin
  pago_cliente := '';
  broker := '';

  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  if SQLQuery2.Active = True then
    SQLQuery2.Close;
    
  if SQLQuery3.Active = True then
    SQLQuery3.Close;

  SQLQuery3.SQL.Text := 'select fnc_nombre from funcionario where fnc_descripcion = ''VENDEDOR'''
    + ' or fnc_descripcion = ''DEFAULT'' order by fnc_nombre';
  
  SQLQuery3.Open;

  DBComboBox1.DataField := 'fnc_nombre';
  DBComboBox1.Clear;

  while not SQLQuery3.EOF do
  begin
    DBComboBox1.Items.Add(SQLQuery3.Fields[0].AsString);
    SQLQuery3.Next;
  end;

  DBComboBox1.Text := '';
  
  ComboBox1.Text := '';

  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
  Edit8.Text := '0';
  Edit9.Text := '';
  Edit10.Text := '';
  Edit11.Text := '';
  Edit12.Text := 'Ninguno';
  descuento := '';
  descuento_cantidad := '';

  Edit1.Enabled := False;
  Edit5.Enabled := False;
  Edit6.Enabled := False;
  Edit7.Enabled := False;
  Edit8.Enabled := False;
  Edit11.Enabled := False;
  
  Button1.Enabled := False;
  Button2.Enabled := False;
  Button3.Enabled := False;
  Button4.Enabled := False;
  Button5.Enabled := False;
  Button6.Enabled := False;
  Button7.Enabled := False;
  Button8.Enabled := False;
  Button9.Enabled := False;
  
  RadioButton1.Enabled := False;
  RadioButton2.Enabled := False;
  RadioButton3.Enabled := False;
  RadioButton4.Enabled := False;
  RadioButton5.Enabled := False;

  ComboBox1.Enabled := False;
  
  Label6.Caption := '';
  
  DBComboBox1.SetFocus;
end;

procedure TForm35.frReport1GetValue(const ParName: String; var ParValue: Variant
  );
begin
  if ParName = 'descuento' then
    ParValue := descuento;
    
  if ParName = 'detalle' then
    ParValue := Edit10.Text;
    
  if ParName = 'total' then
    ParValue := Edit8.Text;
    
  if ParName = 'codigo' then
    ParValue := Label6.Caption;
    
  if ParName = 'nombre' then
    ParValue := Edit2.Text;
    
  if ParName = 'domicilio' then
    ParValue := Edit4.Text;
    
  if ParName = 'cuit' then
    ParValue := Edit3.Text;

  if ParName = 'subtotal' then
    ParValue := subtotal;

  if ParName = 'total_iva' then
    ParValue := total_iva;

  if ParName = 'forma_pago' then
  begin
    if RadioButton1.Checked = true then
      ParValue := 'Contado'
    else
    if RadioButton2.Checked = true then
      ParValue := 'Cuenta Corriente'
    else
      ParValue := 'Tarjeta de Crédito';
  end;
  
  if ParName = 'iva' then
  begin
    if SQLQuery1.Active = true then
      SQLQuery1.Close;
      
    SQLQuery1.SQL.Text := 'select riva_descripcion from responsabilidad_iva where '
      + 'riva_cdg = ''' + Edit9.Text + '''';
      
    SQLQuery1.Open;
    
    while not SQLQuery1.EOF do
    begin
      ParValue := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next;
    end;
    
    SQLQuery1.Close;
  end;
end;

procedure TForm35.LTCPReceive(aSocket: TLSocket);
var
  mensaje : string;
  auxiliar : string;

begin
  if aSocket.GetMessage(mensaje) > 0 then
  begin
    case paso of
      1:
      begin
        nro_conexion := mensaje;
        LTCP.SendMessage('crear_archivo>' + nro_conexion);

        paso += 1;
      end;

      2:
      begin
        archivo_fiscal := mensaje;
        if (Edit9.Text = 'I') and (registro.tipo_impresor = 'Hasar PL8F') then
        begin
          auxiliar := impresor_fiscal.poner_encabezado(Edit2.Text, Edit3.Text, Edit9.Text, Edit4.Text);
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
        if (Edit9.Text = 'I') and (registro.tipo_impresor = 'Hasar P715F')
        or (registro.tipo_impresor = 'Hasar PR4F')then
        begin
          auxiliar := impresor_fiscal.poner_encabezado(Edit2.Text, Edit3.Text, Edit9.Text, Edit4.Text);
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
        if (Edit9.Text <> 'I') and (Edit1.Text <> '1') and
          (registro.tipo_impresor = 'Hasar P715F') or (registro.tipo_impresor = 'Hasar PR4F') then
        begin
          auxiliar := impresor_fiscal.poner_encabezado(Edit2.Text, Edit3.Text, Edit9.Text, Edit4.Text);
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
        if (Edit9.text <> 'C') and (registro.tipo_impresor = 'Epson TM2000') then
        begin
          auxiliar := impresor_fiscal.factabre(Edit2.Text, Edit3.Text, Edit9.Text, Edit4.Text);
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
          LTCP.SendMessage('ping');

        paso += 1;
      end;
      
      3:
      begin
        if (registro.tipo_impresor = 'Hasar PL8F')
        or (registro.tipo_impresor = 'Hasar P715F')
        or (registro.tipo_impresor = 'Hasar PR4F')then
        begin
          if Edit9.Text = 'I' then
            auxiliar := impresor_fiscal.abrir_comprobante('A')
          else
          begin
            if (registro.tipo_impresor = 'Hasar PL8F') or
            ((registro.tipo_impresor = 'Hasar PR4F') and (Edit1.Text <> '1'))
            or ((registro.tipo_impresor = 'Hasar P715F') and (Edit1.Text <> '1')) then
              auxiliar := impresor_fiscal.abrir_comprobante('B')
            else
              auxiliar := impresor_fiscal.abrir_comprobante('T');
          end;

          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
        if (registro.tipo_impresor = 'Epson PR4F') or (registro.tipo_impresor = 'Epson P715F')
        and (Edit9.Text = 'C') then
        begin
          auxiliar := impresor_fiscal.tiqueabre();
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
          LTCP.SendMessage('ping');

         paso += 1;
       end;

      4:
      begin
        if (registro.tipo_impresor = 'Hasar PL8F')
        or (registro.tipo_impresor = 'Hasar PR4F')
        or (registro.tipo_impresor = 'Hasar P715F') then
          auxiliar := impresor_fiscal.referencia_venta(Label6.Caption)
        else
        if (registro.tipo_impresor = 'Epson TM2000') and (Edit9.Text <> 'I') then
          auxiliar := impresor_fiscal.tiquetexto(Label6.Caption)
        else
        if (registro.tipo_impresor = 'Epson TM2000') and (Edit9.Text = 'I') then
          auxiliar := chr(13);

        LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        
        paso += 1;
      end;
      
      5:
      begin
        if SQLQuery2.EOF = false then
        begin
          if (registro.tipo_impresor = 'Hasar PL8F') or
          (registro.tipo_impresor = 'Hasar P715F') or (registro.tipo_impresor = 'Hasar PR4F')  then
            auxiliar := impresor_fiscal.agregar_item(SQLQuery2.Fields[1].AsString,
              SQLQuery2.Fields[0].AsFloat, SQLQuery2.Fields[2].AsFloat, SQLQuery2.Fields[4].AsFloat,
              SQLQuery2.Fields[5].AsFloat)
          else
          if registro.tipo_impresor = 'Epson TM2000' then
          begin
            if Edit9.Text <> 'C' then
              auxiliar := impresor_fiscal.factitem(SQLQuery2.Fields[1].AsString,
                SQLQuery2.Fields[0].AsFloat, SQLQuery2.Fields[2].AsFloat, SQLQuery2.Fields[4].AsFloat,
                SQLQuery2.Fields[5].AsFloat)
            else
              auxiliar := impresor_fiscal.tiqueitem(SQLQuery2.Fields[1].AsString,
                SQLQuery2.Fields[0].AsFloat, SQLQuery2.Fields[2].AsFloat, SQLQuery2.Fields[4].AsFloat,
                SQLQuery2.Fields[5].AsFloat);
          end;
          
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
          
          SQLQuery2.Next;

          paso += 1;
          
          if SQLQuery2.EOF = false then
            paso -= 1;
        end;
      end;

      6:
      begin
        if descuento <> '' then
        begin
          if (registro.tipo_impresor = 'Hasar PL8F') or (registro.tipo_impresor = 'Hasar PR4F')
          or (registro.tipo_impresor = 'Hasar P715F') then
          begin
            if StrToFloat(descuento) > 0 then
              auxiliar := impresor_fiscal.descuento_recargo('recargo', descuento_cantidad)
            else
              auxiliar := impresor_fiscal.descuento_recargo('descuento', descuento_cantidad);
          end
          else
          if registro.tipo_impresor = 'Epson TM2000' then
          begin
            if Edit9.Text = 'C' then
            begin
              if StrToFloat(descuento) > 0 then
                auxiliar := impresor_fiscal.tiquepago('R', descuento_cantidad, 'Recargo ')
              else
                auxiliar := impresor_fiscal.tiquepago('D', descuento_cantidad, 'Descuento ');
            end
            else
            begin
              if StrToFloat(descuento) > 0 then
                auxiliar := impresor_fiscal.factpago('R', descuento_cantidad, 'Recargo ')
              else
                auxiliar := impresor_fiscal.factpago('D', descuento_cantidad, 'Descuento ');
            end;
          end;
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
          LTCP.SendMessage('ping');

        paso += 1;
      end;
      
      7:
      begin
        if (registro.tipo_impresor = 'Hasar PL8F') or (registro.tipo_impresor = 'Hasar PR4F')
        or (registro.tipo_impresor = 'Hasar P715F') then
        begin
          auxiliar := impresor_fiscal.subtotal();
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
          LTCP.SendMessage('ping');
        
        paso += 1;
      end;
      
      8:
      begin
        if (registro.tipo_impresor = 'Hasar PL8F') or (registro.tipo_impresor = 'Hasar PR4F')
        or (registro.tipo_impresor = 'Hasar P715F') then
          auxiliar := impresor_fiscal.total(forma_pago, pago_cliente)
        else
        if registro.tipo_impresor = 'Epson TM2000' then
        begin
          if Edit9.Text = 'C' then
            auxiliar := impresor_fiscal.tiquepago('T', Edit8.Text, forma_pago)
          else
            auxiliar := impresor_fiscal.factpago('T', Edit8.Text, forma_pago)
        end;

        LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        
        paso += 1;
      end;
      
      9:
      begin
        if (registro.tipo_impresor = 'Hasar PL8F') or (registro.tipo_impresor = 'Hasar PR4F')
        or (registro.tipo_impresor = 'Hasar P715F') then
          auxiliar := impresor_fiscal.cerrar_comprobante()
        else
        if registro.tipo_impresor = 'Epson TM2000' then
        begin
          if Edit9.Text = 'C' then
            auxiliar := impresor_fiscal.tiquecierra()
          else
            auxiliar := impresor_fiscal.factcierra(Edit9.Text);
        end;

        LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        
        paso +=1;
      end;
      
      10:
      begin
        LTCP.SendMessage('ejecutar_fiscal>' + archivo_fiscal);

        paso += 1;
      end;

      11:
      begin
        LTCP.SendMessage('borrar_archivo>' +  archivo_fiscal);

        paso += 1;
      end;

      12:
      begin
        LTCP.Disconnect;
      end;
      
    end;
  end;
  
  if paso >= 12 then
    Close;
end;

procedure TForm35.RadioButton1Click(Sender: TObject);
var
  QueryString : String;
  
begin
  if Label6.Caption <> '' then
  begin
    QueryString := 'update venta set vta_tipo = ''CONTADO'' where vta_cdg = ' + Label6.Caption;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
  end;
  
  Edit5.SetFocus;
end;

procedure TForm35.RadioButton2Click(Sender: TObject);
var
  QueryString : String;
  
begin
  if Label6.Caption <> '' then
  begin
    QueryString := 'update venta set vta_tipo = ''CTACTE'' where vta_cdg = ' + Label6.Caption;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
  end;
  
  Edit5.SetFocus;
end;

procedure TForm35.RadioButton3Click(Sender: TObject);
var
  QueryString : String;
  
begin
  if Label6.Caption <> '' then
  begin
    QueryString := 'update venta set vta_tipo = ''TARJETA'' where vta_cdg = ' + Label6.Caption;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
  end;
  
  Edit5.SetFocus;
end;

procedure TForm35.RadioButton4Click(Sender: TObject);
var
  QueryString : String;

begin
  if Label6.Caption <> '' then
  begin
    QueryString := 'update venta set vta_tipo = ''CHEQUE'' where vta_cdg = ' + Label6.Caption;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
  end;
  
  Edit5.SetFocus;
end;

procedure TForm35.RadioButton5Click(Sender: TObject);
var
  QueryString : String;

begin
    if Label6.Caption <> '' then
  begin
    QueryString := 'update venta set vta_tipo = ''DEBITO'' where vta_cdg = ' + Label6.Caption;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
  end;

  Edit5.SetFocus;
end;

initialization
  {$I unit35.lrs}

end.

